/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_cyclonedds/listener_impl/SubscriberListenerImpl.hpp"
#include "dal_cyclonedds/entity_impl/DalSubscriberImpl.hpp"
#include "dal_cyclonedds/entity_impl/DalDataReaderImpl.hpp"
#include "dal_cyclonedds/utils/log.hpp"

static void on_data_on_readers_subscriber(dds_entity_t subscriber, void* arg)
{
    dal_subscriber_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_subscriber_listener_callbacks_t*>(arg);
    if (callbacks_->on_data_on_readers == nullptr)
        return;

    dal_subscriber_t dal_sub;
    DalSubscriberImpl* sub_impl = new DalSubscriberImpl(subscriber, nullptr);
    if (!sub_impl) {
        LOG_ERROR() << "DalSubscriberImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_sub.handle_ = sub_impl;

    void* data = callbacks_->data_;
    (callbacks_->on_data_on_readers)(&dal_sub, data);
    delete sub_impl;
}

static void on_sample_lost_subscriber(dds_entity_t reader, const dds_sample_lost_status_t status, void* arg)
{
    dal_subscriber_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_subscriber_listener_callbacks_t*>(arg);
    if (callbacks_->on_sample_lost == nullptr)
        return;

    dal_datareader_t dal_reader;
    dal_sample_lost_status_t dal_status;
    DalDataReaderImpl* reader_impl = new DalDataReaderImpl(reader, nullptr);
    if (!reader_impl) {
        LOG_ERROR() << "DalDataReaderImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_reader.handle_ = reader_impl;
    StatusConverter::GetInstance()->ConvertSampleLostStatusFromCycloneddsToDal(&status, &dal_status);

    void* data = callbacks_->data_;
    (callbacks_->on_sample_lost)(&dal_reader, &dal_status, data);
    delete reader_impl;
}

static void on_data_available_subscriber(dds_entity_t reader, void* arg)
{
    dal_subscriber_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_subscriber_listener_callbacks_t*>(arg);
    if (callbacks_->on_data_available == nullptr)
        return;

    dal_datareader_t dal_reader;
    DalDataReaderImpl* reader_impl = new DalDataReaderImpl(reader, nullptr);
    if (!reader_impl) {
        LOG_ERROR() << "DalDataReaderImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_reader.handle_ = reader_impl;

    void* data = callbacks_->data_;
    (callbacks_->on_data_available)(&dal_reader, data);
    delete reader_impl;
}

static void on_sample_rejected_subscriber(dds_entity_t reader, 
    const dds_sample_rejected_status_t status, void* arg)
{
    dal_subscriber_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_subscriber_listener_callbacks_t*>(arg);
    if (callbacks_->on_sample_rejected == nullptr)
        return;

    dal_datareader_t dal_reader;
    dal_sample_rejected_status_t dal_status;
    DalDataReaderImpl* reader_impl = new DalDataReaderImpl(reader, nullptr);
    if (!reader_impl) {
        LOG_ERROR() << "DalDataReaderImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_reader.handle_ = reader_impl;
    StatusConverter::GetInstance()->ConvertSampleRejectedStatusFromCycloneddsToDal(&status,
                                                                                &dal_status);

    void* data = callbacks_->data_;
    (callbacks_->on_sample_rejected)(&dal_reader, &dal_status, data);
    delete reader_impl;
}

static void on_liveliness_changed_subscriber(dds_entity_t reader, 
    const dds_liveliness_changed_status_t status, void* arg)
{
    dal_subscriber_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_subscriber_listener_callbacks_t*>(arg);
    if (callbacks_->on_liveliness_changed == nullptr)
        return;
    dal_datareader_t dal_reader;
    dal_liveliness_changed_status_t dal_status;
    DalDataReaderImpl* reader_impl = new DalDataReaderImpl(reader, nullptr);
    if (!reader_impl) {
        LOG_ERROR() << "DalDataReaderImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_reader.handle_ = reader_impl;
    StatusConverter::GetInstance()->ConvertLivelinessChangedStatusFromCycloneddsToDal(&status,
                                                                                   &dal_status);

    void* data = callbacks_->data_;
    (callbacks_->on_liveliness_changed)(&dal_reader, &dal_status, data);
    delete reader_impl;
}

static void on_requested_deadline_missed_subscriber(dds_entity_t reader, 
    const dds_requested_deadline_missed_status_t status, void* arg)
{
    dal_subscriber_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_subscriber_listener_callbacks_t*>(arg);
    if (callbacks_->on_requested_deadline_missed == nullptr)
        return;
    dal_datareader_t dal_reader;
    dal_requested_deadline_missed_status_t dal_status;
    DalDataReaderImpl* reader_impl = new DalDataReaderImpl(reader, nullptr);
    if (!reader_impl) {
        LOG_ERROR() << "DalDataReaderImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_reader.handle_ = reader_impl;
    StatusConverter::GetInstance()->ConvertRequestedDeadlineMissedStatusFromCycloneddsToDal(
        &status, &dal_status);

    void* data = callbacks_->data_;
    (callbacks_->on_requested_deadline_missed)(&dal_reader, &dal_status, data);
    delete reader_impl;
}

static void on_requested_incompatible_qos_subscriber(
    dds_entity_t reader, const dds_requested_incompatible_qos_status_t status, void* arg)
{
    dal_subscriber_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_subscriber_listener_callbacks_t*>(arg);
    if (callbacks_->on_requested_incompatible_qos == nullptr)
        return;
    dal_datareader_t dal_reader;
    dal_requested_incompatible_qos_status_t dal_status;
    DalDataReaderImpl* reader_impl = new DalDataReaderImpl(reader, nullptr);
    if (!reader_impl) {
        LOG_ERROR() << "DalDataReaderImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_reader.handle_ = reader_impl;
    StatusConverter::GetInstance()->ConvertRequestedIncompatibleQosStatusFromCycloneddsToDal(
        &status, &dal_status);

    void* data = callbacks_->data_;
    (callbacks_->on_requested_incompatible_qos)(&dal_reader, &dal_status, data);
    delete reader_impl;
}

static void on_subscription_matched_subscriber(dds_entity_t reader, 
    const dds_subscription_matched_status_t  status, void* arg)
{
    dal_subscriber_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_subscriber_listener_callbacks_t*>(arg);
    if (callbacks_->on_subscription_matched == nullptr)
        return;
    dal_datareader_t dal_reader;
    dal_subscription_matched_status_t dal_status;
    DalDataReaderImpl* reader_impl = new DalDataReaderImpl(reader, nullptr);
    if (!reader_impl) {
        LOG_ERROR() << "DalDataReaderImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_reader.handle_ = reader_impl;
    StatusConverter::GetInstance()->ConvertSubscriptionMatchedStatusFromCycloneddsToDal(&status,
                                                                                     &dal_status);

    void* data = callbacks_->data_;
    (callbacks_->on_subscription_matched)(&dal_reader, &dal_status, data);
    delete reader_impl;
}

SubscriberListenerImpl::SubscriberListenerImpl(dal_subscriber_listener_callbacks_t callbacks)
    : callbacks_(callbacks) {}

SubscriberListenerImpl::~SubscriberListenerImpl()
{
    // c_listener release while participant is releasing.
}

dds_listener_t* SubscriberListenerImpl::CycloneDDSListener()
{
    if (c_listener_ == nullptr) {
        c_listener_ = dds_create_listener(NULL);
    }
    dds_lset_data_on_readers_arg(c_listener_, on_data_on_readers_subscriber, &callbacks_, true);
    dds_lset_sample_lost_arg(c_listener_, on_sample_lost_subscriber, &callbacks_, true);
    dds_lset_data_available_arg(c_listener_, on_data_available_subscriber, &callbacks_, true);
    dds_lset_sample_rejected_arg(c_listener_, on_sample_rejected_subscriber, &callbacks_, true);
    dds_lset_liveliness_changed_arg(c_listener_, on_liveliness_changed_subscriber, &callbacks_, true);
    dds_lset_requested_deadline_missed_arg(c_listener_, on_requested_deadline_missed_subscriber, &callbacks_, true);
    dds_lset_requested_incompatible_qos_arg(c_listener_, on_requested_incompatible_qos_subscriber, &callbacks_, true);
    dds_lset_subscription_matched_arg(c_listener_, on_subscription_matched_subscriber, &callbacks_, true);
    return c_listener_;
}